﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using System.Collections;
using System.IO;
using School.Admission.ReportsCls;

namespace School.Admission
{
    public class Class_Student
    {
        private static SchoolEntities db = new SchoolEntities();

        public static Int64 Add(TE_Class_Student  ClassStudent)
        {
            try
            {
                db.AddToTE_Class_Student(ClassStudent);
                db.SaveChanges();
                return ClassStudent.Id;
            }
            catch
            {
                return -1;
            }
        }
        public static TE_Class_Student  Single(long id)
        {
            try
            {
                return db.TE_Class_Student.Single(x => x.Id == id);
            }
            catch
            {
                return null;
            }
        }


        public static IEnumerable<TE_Class_Student> All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from ClassStudent in db.TE_Class_Student
                        select ClassStudent;
            foreach (DictionaryEntry item in items)
            {
                if (item.Key == "Class_Id")
                {
                    long Class_Id;
                    Class_Id = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.Class_Id == Class_Id);
                }
                if (item.Key == "Student_Id")
                {
                    long Student_Id;
                    Student_Id = Convert.ToInt32(item.Value);
                    query = query.Where(c => c.Student_Id == Student_Id);
                }
            }
            return query.ToList();
        }

        public static void Update(TE_Class_Student ClassStudent)
        {

            TE_Class_Student c = db.TE_Class_Student.Single(x => x.Id == ClassStudent.Id);
            c.Id = ClassStudent.Id;
            c.Class_Id = ClassStudent.Class_Id;
            c.Student_Id = ClassStudent.Student_Id;
            
            db.SaveChanges();

        }

        public static Boolean Delete(long id)
        {
            try
            {
                db.TE_Class_Student.DeleteObject(db.TE_Class_Student.Single(x => x.Id == id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public IQueryable<rptStudentClasscls> GetAllStudentsinClass(int? Id,int eductionalyr)
        {
            var query = from Tclass in db.TE_Class_Student 
                        where Tclass.EductionalYear_Id==eductionalyr
                        select new ReportsCls.rptStudentClasscls
                        {
                            Class_Id = Tclass.Class_Id,
                            studentFullName = Tclass.TE_Student.Ar_FirstName+ " " + Tclass.TE_Student.Ar_SecondName + " " + Tclass.TE_Student.Ar_ThirdNmae + " " + Tclass.TE_Student.Ar_FourthName,
                            Campus = Tclass.ORG_Class.ORG_Grade.ORG_Stage.ORG_Certificate.ORG_Campus.Name,
                            Certificate = Tclass.ORG_Class.ORG_Grade.ORG_Stage.ORG_Certificate.Name,
                            Stage = Tclass.ORG_Class.ORG_Grade.ORG_Stage.Name,
                            Grade = Tclass.ORG_Class.ORG_Grade.Name,
                            ClassCode = Tclass.ORG_Class.Code,
                            ClassName = Tclass.ORG_Class.Name,
                            studentRegNo = Tclass.TE_Student.RegistrationNumber,
                            studentFileNumber = Tclass.TE_Student.FileNumber

                        };
            if(Id != null)
            {
            query.Where( c => c.Class_Id == Id);
            }
            return query;

        }
    }
}
